package com.sinaapp.mrblog.game.Assasin.model;

import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.MathUtils;
import com.sinaapp.mrblog.game.Assasin.stage.GameStage;
import com.sinaapp.mrblog.game.Assasin.util.AssasinConst;
import com.sinaapp.mrblog.game.Assasin.util.AssasinManager;


public class Policeman extends BaseActor{
	
	public static final int POLICEMAN_STATE_IDLE = 0;
	public static final int POLICEMAN_STATE_PATROL = 1;
	public static final int POLICEMAN_STATE_ALERT_PATROL = 2;
	public static final int POLICEMAN_STATE_ALERT_IDLE = 3;
	public static final int POLICEMAN_STATE_ALERT_SHOOT = 4;
	
	public static final float PATROL_VELOCITY = 6;
	public static final float ALERT_VELOCITY = PATROL_VELOCITY*2f;
	public static final float POLICEMAN_WIDTH = 2;
	public static final float POLICEMAN_HEIGHT = 1;
	public static final float TOUCH_FLOOD_TIME = 1.5f;
	
	public DetectionSector detectionSector;
	
	float touchFloodTime;
	float changeAngle;
	float changeStateTime;
	float changeAngleTime;
	float shootTime;
	
	
	public Policeman(float x, float y){
		super(x,y,POLICEMAN_WIDTH,POLICEMAN_HEIGHT);
		detectionSector = new DetectionSector(this);
		setState(POLICEMAN_STATE_IDLE);
	}

	@Override
	public void act(float delta) {
		region = AssasinManager.policemanIdleRegion;
		update(delta);
		if(detectionSector.killerIsDiscover&&GameStage.killer.getState()!=Killer.KILLER_STATE_DEAD){
			GameStage.listener.alert();
			setState(POLICEMAN_STATE_ALERT_SHOOT);			
		}
		
		if(detectionSector.bossIsDiscover&&GameStage.boss.getState()==Boss.BOSS_STATE_DEAD){
			GameStage.listener.alertByBossDead();
		}
		
		if(getState()==POLICEMAN_STATE_IDLE){
			region = AssasinManager.policemanIdleRegion;
			vel.set(0, 0);
		}else if(getState() == POLICEMAN_STATE_PATROL){
			region = AssasinManager.policemanPatrol_AN.getKeyFrame(getStateTime(),true);
			vel.x = PATROL_VELOCITY * MathUtils.cosDeg(angle);
			vel.y = PATROL_VELOCITY * MathUtils.sinDeg(angle);
		}else if(getState() == POLICEMAN_STATE_ALERT_PATROL){
			region = AssasinManager.policemanAlertPatrol_AN.getKeyFrame(getStateTime(),true);
			vel.x =  ALERT_VELOCITY* MathUtils.cosDeg(angle);
			vel.y = ALERT_VELOCITY * MathUtils.sinDeg(angle);
		}else if(getState()== POLICEMAN_STATE_ALERT_IDLE){
			region = AssasinManager.policemanAlertIdleRegion;
		}else if(getState() == POLICEMAN_STATE_ALERT_SHOOT){
			region = AssasinManager.policemanAlertShootRegion;
			angle = MathUtils.atan2(GameStage.killer.getCenterY()-getCenterY(),
					GameStage.killer.getCenterX()- getCenterX())*MathUtils.radiansToDegrees;
			if(shootTime>AssasinConst.POLICEMAN_SHOOT_INTERVAL_TIME){
				GameStage.listener.alert();
				GameStage.listener.shoot(new Bullet(this));
				shootTime =0;
			}
		}
		
		shootTime += delta;
		vel.scl(delta);
		super.act(delta);
	}
	
	
	private void update (float delta){
		angle += changeAngle * delta;		
		if(getStateTime()>changeStateTime){
			switch (getState()) {
			case POLICEMAN_STATE_IDLE:
				setState(POLICEMAN_STATE_PATROL);
				break;
			case POLICEMAN_STATE_PATROL:
				setState(POLICEMAN_STATE_IDLE);
				break;
			case POLICEMAN_STATE_ALERT_PATROL:
				setState(POLICEMAN_STATE_ALERT_IDLE);
				changeAngleTime *= AssasinConst.POLICEMAN_ALERT_CHANGE_RATE;
				break;
			case POLICEMAN_STATE_ALERT_IDLE:
				setState(POLICEMAN_STATE_ALERT_PATROL);
				changeAngleTime *= AssasinConst.POLICEMAN_ALERT_CHANGE_RATE;
				break;
			}
		}
	}

	@Override
	public void moveImpeded() {
		if(MathUtils.random(1)>0.5)
			angle += AssasinConst.POLICEMAN_CHANGE_ANGLE_SCOPE;
		else 
			angle -= AssasinConst.POLICEMAN_CHANGE_ANGLE_SCOPE;
		super.moveImpeded();
	}

	@Override
	public void setState(int state) {
		changeStateTime = MathUtils.random(AssasinConst.POLICEMAN_CHANGE_STATE_MINTIME, AssasinConst.POLICEMAN_CHANGE_STATE_MAXTIME);
		changeAngle = MathUtils.random(-AssasinConst.POLICEMAN_CHANGE_ANGLE_SCOPE,AssasinConst.POLICEMAN_CHANGE_ANGLE_SCOPE);
		super.setState(state);
	}

	@Override
	public void refreshStatus() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public TextureRegion getReadyRegion() {
		// TODO Auto-generated method stub
		return AssasinManager.policemanIdleRegion;
	}
	
	
	
}
